<template>
    <div class="setting-content basicSetting-container">
        <h2 class="setting-header">
            <strong>基本设置</strong>
        </h2>
        <el-form class="setting-form"  :model="formData" label-width="100px"  size="large" :rules="validationRules" ref="formRef">
            <el-form-item label="系统名称" prop="systemName">
                <el-input placeholder="输入系统名称" v-model="formData.systemName"></el-input>
            </el-form-item>
            <el-form-item label="官网地址" prop="officialWebsiteUrl">
                <el-input placeholder="输入官网地址" v-model="formData.officialWebsiteUrl"></el-input>
            </el-form-item>
            <el-form-item label="LOGO" prop="logoUrl" >
                <div class="form-img" @click="handleUpload('logoUrl')">
                    <div class="has-img" v-if="formData.logoUrl">
                      <img :src="formData.logoUrl" alt="" />
                      <span @click.stop="handleRemoveImg('thumb')">
                          <el-icon><Delete /></el-icon>
                      </span>
                    </div>
                    <el-icon v-else><Plus /></el-icon>
                </div>
            </el-form-item>
            <el-form-item label="版权信息" prop="copyrightInfo">
                <el-input placeholder="输入版权信息" v-model="formData.copyrightInfo"></el-input>
            </el-form-item>
            <el-form-item label="ICP备案" prop="icpRecordNumber">
                <el-input placeholder="输入ICP备案" v-model="formData.icpRecordNumber"></el-input>
                <el-input placeholder="输入链接" v-model="formData.icpRecordUrl"></el-input>
            </el-form-item>
            <el-form-item label="公网安备" prop="beianRecordNumber">
                <el-input placeholder="输入公网安备" v-model="formData.beianRecordNumber"></el-input>
                <el-input placeholder="输入链接" v-model="formData.beianRecordUrl"></el-input>
            </el-form-item>
            <div class="fotter">
                <el-button type="primary" @click="handleSubmit" size="default">保存</el-button>
                <el-button @click="resetForm" size="default" class="ml-2">
                    重置
                </el-button>
            </div>
        </el-form>
        <!-- 图片上传  -->
        <FilesUpload v-if="showFilesUpload" v-model:showDialog="showFilesUpload" :isPublic="true" :modelId="1" :limit="1" @getFileList="insertFiles" :isToJpg="true"></FilesUpload>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from "element-plus";
import { Delete, Plus } from '@element-plus/icons-vue'
import { FilesUpload } from "@ruifox-cms/components"
//获取基本设置信息
// 表单数据结构
const formData = reactive({
  systemName: '',
  officialWebsiteUrl: '',
  logoUrl: '',
  copyrightInfo: '',
  icpRecordNumber: '',
  icpRecordUrl: '',
  beianRecordNumber: '',
  beianRecordUrl: ''
})
// 表单初始值（用于重置）
const initialValues = reactive({ ...formData })
// 表单验证规则
const validationRules = reactive({
  systemName: [
    { required: true, message: '请输入系统名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  officialWebsiteUrl: [
    { required: true, message: '请输入官网地址', trigger: 'blur' },
    { 
      pattern: /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([\/\w.-]*)*\/?$/,
      message: '请输入有效的URL地址',
      trigger: 'blur' 
    }
  ],
  logoUrl: [
    { required: false, trigger: 'change' } // 可选字段
  ],
  copyrightInfo: [
    { required: true, message: '请输入版权信息', trigger: 'blur' },
    { max: 200, message: '最多输入200个字符', trigger: 'blur' }
  ],
  icpRecordNumber: [
    { required: true, message: '请输入ICP备案号', trigger: 'blur' }
  ],
  icpRecordUrl: [
    { required: true, message: '请输入ICP备案链接', trigger: 'blur' },
    { type: 'url', message: '请输入有效的URL', trigger: 'blur' }
  ],
  beianRecordNumber: [
    { required: true, message: '请输入公网安备号', trigger: 'blur' }
  ],
  beianRecordUrl: [
    { required: true, message: '请输入公网安备链接', trigger: 'blur' },
    { type: 'url', message: '请输入有效的URL', trigger: 'blur' }
  ]
})

// 获取表单引用
const formRef = ref(null)

// 模拟API数据获取
const fetchSettings = async () => {
  try {
    // 实际项目中替换为真实API调用
    const mockData = {
      systemName: '企业招聘系统',
      officialWebsiteUrl: 'https://example.com',
      logoUrl: '',
      copyrightInfo: '© 2025 企业招聘系统 版权所有',
      icpRecordNumber: '粤ICP备123456号',
      icpRecordUrl: 'https://beian.miit.gov.cn',
      beianRecordNumber: '粤公网安备12345678901234号',
      beianRecordUrl: 'https://www.beian.gov.cn'
    }
    
    // 填充表单数据
    Object.assign(formData, mockData)
    
    // 更新初始值用于重置
    Object.assign(initialValues, mockData)
    
  } catch (error) {
    ElMessage.error('获取配置信息失败: ' + error.message)
  }
}
// 表单提交处理
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 模拟API提交
      console.log('提交表单数据:', formData)
      
      // 实际项目中替换为真实API调用
      // saveSettings(formData).then(() => {
      //   ElMessage.success('设置保存成功')
      // }).catch(error => {
      //   ElMessage.error('保存失败: ' + error.message)
      // })
      
      ElMessage.success('设置保存成功')
    } else {
      ElMessage.error('表单验证失败，请检查输入')
      return false
    }
  })
}

// 重置表单
const resetForm = () => {
  formRef.value.resetFields()
  Object.assign(formData, initialValues)
  ElMessage.info('表单已重置')
}
//图片上传
const showFilesUpload = ref(false)
const resourceFormList = ref([])
const insertFiles = filesList => {
  resourceFormList.value = filesList.map(item => {
    return {
      resourceData: {
        title: item.name,
        url: item.url,
      },
      needUpload: item.needUpload,
      tagList: item.tagList || [],
    }
  })
  console.log("resourceFormList.value111",resourceFormList.value);
  formData.logoUrl = resourceFormList.value[0].resourceData.url
  
}
const handleUpload = async () => { 
  showFilesUpload.value = true;
};
const handleRemoveImg = () => {
  formData.logoUrl = ''
}

// 组件挂载后获取数据
onMounted(() => {
    fetchSettings()
})
</script>
<style lang="scss" scoped>
      .setting-form{
    width: 500px;
    max-width: 98%;
    ::v-deep *{
      font-size: 14px;

    }
    ::v-deep svg{
      font-size: 16px;

    }
    .item-row{
      display: flex;
      align-items: center;
      span{
        margin-right: 10px;
        color: $black_2;
        font-size: 14px;
      }
      ::v-deep .el-radio{
        margin-right: 10px;
      }
    }
  }
  ::v-deep .el-radio{
    margin-right: 10px;
  }
  ::v-deep .el-input+.el-input{
    margin-top: 10px;
  }
  .fotter{
    display: flex;
    justify-content: end;
  }
  .form-img{
    height: 100px;
    width: 150px;
    border: 1px dashed $borderColor;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 10px;
    .has-img{
      height: 100%;
      width: 100%;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      img{
        max-width: 100%;
        max-height: 100%;
      }
      span{
        display: none;
        position: absolute;
        right: 0;
        top: 0;
        background-color: $failColor;
        height: 20px;
        width: 20px;
        font-size: 18px;
        color: #fff;
      }
      &:hover{
        span{
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
    } 
  }
  .notice-type{
    display: flex;
    flex-direction: column;
  }
  .add-but{
    margin-top: 10px;
    display: flex;
    align-items: center;
  }
  .del-icon{
    cursor: pointer;
  }
</style>